package com.leave.dao;

import com.leave.entity.Department;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DepartmentDao {
    private JdbcTemplate jdbcTemplate;
    public List<Department> query(Department department){
        String sql ="select * from sys_department where 1=1 ";
        List<Object> paramValues=new ArrayList<>();
        List<Department> departmentList=new ArrayList<>();
        if(department!=null){
            if(department.getDepID()!=null&&!"".equals(StringUtils.trim(department.getDepID()))){
                sql+="and depID=?";
                paramValues.add(department.getDepID());
            }
        }
        departmentList =jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Department>() {
            @Override
            public Department mapRow(ResultSet rs, int rowNum) throws SQLException {
                Department department1=new Department();
                String id =rs.getString("depID");
                String name =rs.getString("depName");
                department1.setDepID(id);
                department1.setDepName(name);
                return department1;
            }
        });
        return departmentList;
    }

    public List<Map<String, Object>> selectList(String keyword) throws SQLException {
        String sql = "SELECT * FROM sys_department WHERE depID LIKE ? OR depName LIKE ?";
        String likeKeyword = "%" + keyword + "%";
        String[] paramValues = {likeKeyword, likeKeyword};
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String, Object>>() {
            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map<String, Object> map = new HashMap<>();
                int columnCount = rs.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    map.put(columnName, columnValue);
                }
                return map;
            }
        };
        return jdbcTemplate.query(sql, paramValues, rowMapper);
    }

    public void delete(String depID){
        String sql ="delete from sys_department where depID=?";
        String[]paramValues={depID};
        jdbcTemplate.update(sql,paramValues);
    }
    public void insert(Department department) {
        //声明插入SQL字符串变量sql
        String sql = "insert into sys_department(depID,depName)values(?,?)";
        if (department != null) {
            String[] paramValues = {department.getDepID(), department.getDepName()};
            jdbcTemplate.update(sql, paramValues);
        }
    }
    public void update(Department department){
        //声明插入SQL字符串变量sql
        String sql = "update sys_department set depName=? WHERE depID=?";
        if (department != null) {
            String[] paramValues = {department.getDepName(),department.getDepID()};

            jdbcTemplate.update(sql, paramValues);
        }
    }
    /**
     *  setter注入方式，注意不需要定义getter方法。
     * @param jdbcTemplate
     */
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
